// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import {PayRecordList} from '../domains/payrecordlist.domain';
import { PayRecord } from '../domains/PayRecord.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class PayRecordServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'PayRecordServiceSvr.assx/';
  }


  getListCount(token: string, status: number, customerId: string, customerName: string, isPostone: number, type: number, startTime: Date, endTime: Date): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('status', status.toString())
      .append('customerId', customerId.toString())
      .append('customerName', customerName.toString())
      .append('isPostone', isPostone.toString())
      .append('type', type.toString())
      .append('startTime', startTime.toString())
      .append('endTime', endTime.toString());
    return this.request.post<number>(this.baseUrl + 'getListCount', httpParams).then((res) => {
      return res;
    });
  }


  pay(token: string, payRecordId: string, actualMoney: number, description: string, payWay: number): Promise<string> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('payRecordId', payRecordId.toString())
      .append('actualMoney', actualMoney.toString())
      .append('description', description.toString())
      .append('payWay', payWay.toString());
    return this.request.post<string>(this.baseUrl + 'pay', httpParams).then((res) => {
      return res;
    });
  }


  updatePaymentRecord(token: string, payRecordId: string, actualMoney: number, description: string, payWay: number): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('payRecordId', payRecordId.toString())
      .append('actualMoney', actualMoney.toString())
      .append('description', description.toString())
      .append('payWay', payWay.toString());
    return this.request.post<number>(this.baseUrl + 'updatePaymentRecord', httpParams).then((res) => {
      return res;
    });
  }


  listPaymentRecord(token: string, status: number, customerId: string, customerName: string, houseId:string, startTime: string, endTime: string, isPostone: number, type: number, skip: number, size: number, option:number): Promise<PayRecordList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('status', status.toString())
      .append('customerId', customerId.toString())
      .append('customerName', customerName.toString())
      .append('houseId',houseId.toString())
      .append('startTime', startTime)
      .append('endTime', endTime)
      .append('isPostone', isPostone.toString())
      .append('type', type.toString())
      .append('skip', skip.toString())
      .append('size', size.toString())
      .append('option',option.toString());
    return this.request.post<PayRecordList>(this.baseUrl + 'listPaymentRecord', httpParams).then((res) => {
      return new Promise((resove) =>{
        resove(res)
      })
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  getPaymentRecord(token: string, id: string): Promise<PayRecord> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<PayRecord>(this.baseUrl + 'getPaymentRecord', httpParams).then((res) => {
      return new PayRecord(res);
    });
  }


  savePaymentRecord(token: string, contractId: string, startTime: Date, endTime: Date, type: number, originalPrice: number, actualMoney: number, customerName: string, customerId: string, description: string, payWay: number, payPeriod: number): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('contractId', contractId.toString())
      .append('startTime', startTime.toString())
      .append('endTime', endTime.toString())
      .append('type', type.toString())
      .append('originalPrice', originalPrice.toString())
      .append('actualMoney', actualMoney.toString())
      .append('customerName', customerName.toString())
      .append('customerId', customerId.toString())
      .append('description', description.toString())
      .append('payWay', payWay.toString())
      .append('payPeriod', payPeriod.toString());
    return this.request.post<number>(this.baseUrl + 'savePaymentRecord', httpParams).then((res) => {
      return res;
    });
  }
}
